//basic
function _g(id){
  return document.getElementById(id);
}
function _gn(a){
  return document.getElementsByName(a);
}
function _unesc(a) {
  return window.decodeURIComponent ? decodeURIComponent(a) : unescape(a)
}
function arrayIndexOf(arr, obj, opt_fromIndex){
      var fromIndex = opt_fromIndex == null ?
          0 : (opt_fromIndex < 0 ?
               Math.max(0, arr.length + opt_fromIndex) : opt_fromIndex);

      for (var i = fromIndex; i < arr.length; i++) {
        if (i in arr && arr[i] === obj)
          return i;
      }
      return -1;
}
function getElementsByClass(elementClass, context){
  if(elementClass == null){
    alert("element class is null.");
    return null;
  }
  if(context == null) context = document;
  
  var els = context.getElementsByTagName('input' || '*');

  if (elementClass) {
    var arrayLike = {};
    var len = 0;
    for (var i = 0, el; el = els[i]; i++) {
      var className = el.className;
      // Check if className has a split function since SVG className does not.
      if (typeof className.split == 'function' &&
          arrayIndexOf(className.split(/\s+/), elementClass) > -1) {
        arrayLike[len++] = el;
      }
      
    }
    arrayLike.length = len;
    return arrayLike;
  } else {
    return els;
  }
}
function _get(elementClass, context){
  var els = getElementsByClass(elementClass, context);
  return (els && els.length > -1) ? els[0] : null;
}
function getElementsByNameLike(name, opt_tag, opt_el) {
  var parent = opt_el || document;
  var tagName = (opt_tag && opt_tag != '*') ? opt_tag.toUpperCase() : '';
  var els = parent.getElementsByTagName(tagName || '*');
  var arrayLike = {};
  var len = 0;
  for(var i = 0, el;el = els[i]; i++){
   if(el.name &&  new RegExp(name).test(el.name))
    arrayLike[len++] = el;
  }
  return arrayLike;
}

//form
function getParentForm(node) {
  var found = false;
  while (!found) {
    if(node == null) return null;
      if (node.nodeName == 'FORM') {
          found = true;
          return node;
      }
      node = node.parentNode;
  }
  return null;
}
function getPageHtml(currentNode){
  var pageHtml = {};
  var parentForm = getParentForm(currentNode);
  if(!parentForm){
    alert("cannot find parent form.");
    return false;
  }
  var eOrderBy = _get("orderBy", parentForm);
  var eOrder = _get("order", parentForm);
  var ePageNo = _get("pageNo", parentForm);
  if(!eOrderBy || !eOrder || !ePageNo){
    alert("cannot find orderBy or order or pageNo element.");
    return false;
  }
  pageHtml.form = parentForm;
  pageHtml.orderBy = eOrderBy;
  pageHtml.order = eOrder;
  pageHtml.pageNo = ePageNo;
  return pageHtml;
}
function jumpPage(pageNo, currentNode) {
  var pageHtml = getPageHtml(currentNode);
  if(!pageHtml) return false;
  
  pageHtml.pageNo.value = pageNo;
  pageHtml.form.submit();
}
function sort(orderBy, currentNode) {
  var pageHtml = getPageHtml(currentNode);
  if(!pageHtml) return false;
  if (pageHtml.orderBy.value == orderBy) {
    if (pageHtml.order.value == "") {
      pageHtml.order.value = "asc";
    }
    else if (pageHtml.order.value == "desc") {
      pageHtml.order.value = "";
    }
    else if (pageHtml.order.value == "asc") {
      pageHtml.order.value = "desc";
    }
  }
  else {
    pageHtml.orderBy.value = orderBy;
    pageHtml.order.value = "asc";
  }

  pageHtml.form.submit();
}

function search(currentNode) {
  var pageHtml = getPageHtml(currentNode);
  if(!pageHtml) return false;
  pageHtml.orderBy.value = "";
  pageHtml.order.value = "";
  pageHtml.pageNo.value = "1";

  pageHtml.form.submit();
}

function submitMethod(method, formId){
  var f = _g(formId);
  if(!f){
    alert("cannot find form "+formId+".");
    return false;
  }
  var eMethod = _get("method", f);
  if(!eMethod){
    alert("cannot find method element.");
    return false;
  }
  eMethod.value=method;
  f.submit();
}



//dropdown
function showDropDown(dropDownData,codeElementName,defaultOption,containerPrefix,dropPrefix){
  for(var i=0;i<codeElementName.length;i++)
    showx(i,codeElementName,defaultOption,containerPrefix,dropPrefix);
}
  
function equalInnerHTML(html,callback){
  var f = document.createDocumentFragment();
  var isTable = html.indexOf("<tbody") > -1;
  //var d = isTable?document.createElement("table"):document.createElement("div");
  var d = document.createElement("div");
  //if(isTable) alert(d.outerHTML);
  d.innerHTML = html;
  (function(){
    if(d.firstChild){
      f.appendChild(d.firstChild);
      setTimeout(arguments.callee,0)
    }else{
      if(isTable)
        callback(f.firstChild.firstChild);
      else
        callback(f.firstChild);
    }
  })()
}
function findSubList(data,value){
  var list = [];
  if(data == null) return list;
  for(var i =0; i < data.length;i++){
    if(data[i].code == value)
       list = data[i].list;
  }
  return list;
}

function getDataByGrade(x,codeElementName){
  var list = dropDownData;
  if(list == null) list = [];
  for(var i = 0;i<x&&list&&list.length>0;i++){
    list = findSubList(list,_gn(codeElementName[i])[0].value)
  }
  return list;
}

function showx(x,codeElementName,defaultOption,containerPrefix,dropPrefix){
  if(containerPrefix == null ) containerPrefix = "dropContainer";
  if(dropPrefix == null ) dropPrefix = "drop";
  if(defaultOption == null) defaultOption = '<option value="0">&#20840;&#37096;</option>';
  var list = getDataByGrade(x,codeElementName);
  //if(x==2) alert(list);
  var newSelect = '<select id="'+dropPrefix+'_'+x+'">'+defaultOption;
  for(var i =0; list && i < list.length;i++){
    newSelect +='<option value="'+list[i]["code"]+'"'
    +(_gn(codeElementName[x])[0].value + "" == list[i]["code"]?' selected':"")
    +'>'+list[i]["name"]+'</option>'; 
  }
  newSelect += '</select>';
  _g(containerPrefix+"_"+x).removeChild(_g(dropPrefix+"_"+x));
  equalInnerHTML(newSelect,function(f){
    _g(containerPrefix+"_"+x).appendChild(f);
    _g(dropPrefix+"_"+x).onchange=function(event){
      var drop = (window.event && window.event.srcElement) ? window.event.srcElement : event.target;
      var num = parseInt(drop.id.substring(dropPrefix.length + 1));
      _gn(codeElementName[num])[0].value = drop.options[drop.selectedIndex].value;
      for(var j = num+1;j<codeElementName.length;j++){
        _gn(codeElementName[j])[0].value = "";
      }
      for(var k = num+1;k<codeElementName.length;k++){
        showx(k,codeElementName,defaultOption,containerPrefix,dropPrefix);
      }
    }
  })
}
//ajax  
function _trim( text ) {
  return (text || "").replace( /^\s+|\s+$/g, "" );
}
function _e(s) {
  return escape(escape(s));
}
function now(){
  return +new Date;
}
function _ajax(url,process){
  var xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
/*    */
  try {
    //xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  } catch(e){}
  var ts = now();
  // try replacing _= if it is there
  var ret = url.replace(/(\?|&)_=.*?(&|$)/, "$1_=" + ts + "$2");
  // if nothing was replaced, add timestamp to the end
  url = ret + ((ret == url) ? (url.match(/\?/) ? "&" : "?") + "_=" + ts : "");
//url = encodeURI(url);
  xhr.open("GET", url, true);
  xhr.onreadystatechange = function(){
    if(xhr.readyState==4){
      process(xhr);
    }
  }
  xhr.setRequestHeader( "Content-Type", "text/html;charset=GBK" );
  xhr.send(null);
}
function bindSelect(selectId,inputId){
  var selectElement = _g(selectId);
  var inputElement = _g(inputId);
  var x = (inputElement && inputElement.value) ? inputElement.value : null;
  var os = selectElement.options;
  if(x && os && os.length)
    for(var i = 0;i< os.length;i++){
      if(os[i].value == x) os[i].selected = true;
    }
  selectElement.onchange = function(){
    inputElement.value = os[selectElement.selectedIndex].value;
  }
}